<?php

function getWF($y,$m){
		return idate("w",mktime(0,0,0,$m,1,$y));
}

function getTS($y,$m){
	return idate("t",mktime(0,0,0,$m,1,$y));
}

function getW($y,$m,$d){
	return idate("w",mktime(0, 0, 0, $m,$d,$y));
}
function getFD($y,$m,$d){
	return date("Ymd",mktime(0, 0, 0, $m,$d,$y));
}

function get_date_data($date)
{
	return false;
}

function displayKalendar(){

	$y     = $_GET["year"]!=''?$_GET["year"] : date("Y");
	$m     = $_GET["mon"]!=''?$_GET["mon"] : date("m");
	$today = date("Y").date("m").date("d");

	if(isset($_GET['c_get_time']))
	{
		$y=date('Y',$_GET['c_get_time']);
		$m=date('m',$_GET['c_get_time']);
	}

	if($m == 1){
		$previousy = $y-1;
		$url_left = 'mon=12&amp;year='.$previousy.'';
	}else{
		$url_left = 'mon='.($m-1).'&amp;year='.$y.'';
	}

	if($m == 12){
		$nexty = $y+1;
		$url_right = 'mon=1&amp;year='.$nexty.'';
	}else{
		$url_right = 'mon='.($m+1).'&amp;year='.$y.'';
	}

	$content = '';

	$content .= '
		<table  border="0" cellspacing="1" cellpadding="5" width="100%">
		<tr>
		   <th class="whiteBg"><a rel="'.$url_left.'" onclick="js_data_change(this.rel); return false" href="'.$url_left.'">&laquo;</a></th>
		   <th colspan="5" class="whiteBg">'.date("M", mktime(0, 0, 0, $m, 1, $y))." ".$y.'</th>
		   <th class="whiteBg"><a onclick="js_data_change(this.rel); return false" href="'.$url_right.'" rel="'.$url_right.'">&raquo;</a></th>
		</tr>
		<tr>
		   <th>So</th>
		   <th>Mo</th>
		   <th>Di</th>
		   <th>Mi</th>
		   <th>Do</th>
		   <th>Fr</th>
		   <th>Sa</th>
		</tr>
		<tr class="calendar_tr">';
		$Calendar = 1;
		for($i = getTS($y,$m-1)-(getWF($y,$m)-1);$i <= getTS($y,$m-1);$i++){
		   $content .= '<td class="pre weekday_'.getW($y,$m-1,$i).'"/>';

		}

		for($i=1;$i <= 7-getWF($y,$m);$i++){
               $m_calendar=($Calendar++);
           if($today == getFD($y,$m,$m_calendar)){
			 $content .= "<td class='today weekday_".getW($y,$m,($m_Calendar-1))."'><a rel=\"".mktime(0,0,0,$m,$m_calendar,$y)."\" onclick=\"retrieve_data_record(this); return false\" href=\"#\">".$m_calendar."</a></td>";
		   }else{
			 $content .= "<td class='weekday_".getW($y,$m,($Calendar-1))."'><a onclick=\"retrieve_data_record(this); return false\" rel=\"".mktime(0,0,0,$m,$m_calendar,$y)."\" href='#'>".$m_calendar."</a></td>";
		   }
		}

	$content .= '</tr>';
	static $c=0;
	for($i=2;$i<7;$i++){
	   $content .= '<tr class="calendar_tr">';

	   	for($j=0;$j<7;$j++){
			if($Calendar > getTS($y,$m)){

                $_o_m=$m==12?1:($m+1);
                $_o_y=$m==12?($y+1):$y;


                if(getW($y,$m,getTS($y,$m))!=6&&$c==0)
                {
					if(getW($_o_y,$_o_m,($Calendar-getTS($y,$m)))==6)
					{
						$c++;
					}

                        $content.='<td class="weekday_'.getW($_o_y,$_o_m,($Calendar-getTS($y,$m))).'"/>';

                }else{
                    $content.='<td/>';
                }

			}else{
				$date_str = $y."-".$m."-".$Calendar;
				if(get_date_data($date_str))
				{
					$date_link = '<a onclick="retrieve_data_record(this); return false" rel="'.mktime(0,0,0,$m,$Calendar,$y).'" href="dates.php?id=52&date='.$date_str.'">'.$Calendar.'</a>';
					$class = getW($y,$m,$Calendar);
                    $date_link='weekday_'.getW($y,$m,$Calendar);
				}
				else
				{
					$date_link = '<a onclick="retrieve_data_record(this); return false" rel="'.mktime(0,0,0,$m,$Calendar,$y).'" href="#">'.$Calendar.'</a>';
					$class = 'weekday_'.getW($y,$m,$Calendar);
				}
				if($today==getFD($y,$m,$Calendar)){
					 $content .= "<td class='today ".$class."'>".$date_link."</td>";
				}else{
					 $content .= "<td class='week".$j." ".$class."'>".$date_link."</td>";
				}
			}
			$Calendar++;
	   }
		 $content .= '</tr>';
	  }
	  $content .= '</table>';


		return $content;
	}

	echo  displayKalendar();
?>